package com.example.taco_message_receive.messing.jms;

import com.example.taco_message_receive.messing.Order;
import com.example.taco_message_receive.messing.OrderReceive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;

@Component
public class JmsOrderReceiver implements OrderReceive {

    private JmsTemplate jms;

    private MessageConverter converter;

    @Autowired
    public JmsOrderReceiver(JmsTemplate jms, MessageConverter converter) {
        this.jms = jms;
        this.converter = converter;
    }

    @Override
    public Order receiveOrder() throws JMSException {
        return (Order) jms.receiveAndConvert("tacocloud.order.taco"); //自动转换
    }

//    @Override
//    public Order receiveOrder() throws JMSException {
//        Message message = jms.receive("tacocloud.order.queue"); // 未转换的message
//        return (Order)converter.fromMessage(message);
//    }
}
